package sa.com.sure.ejtimaa.components;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import sa.com.sure.ejtimaa.R;
import sa.com.sure.ejtimaa.common.Helper;
import sa.com.sure.ejtimaa.database.MeetingAttachments;
import sa.com.sure.ejtimaa.database.MeetingItemsAttachments;
import sa.com.sure.ejtimaa.model.MeetingAttachmentInfo;
import sa.com.sure.ejtimaa.model.MeetingInfo;
import sa.com.sure.ejtimaa.model.MeetingItemInfo;
import sa.com.sure.ejtimaa.model.MeetingItemsAttachmentInfo;
import android.content.Context;
import android.database.SQLException;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MeetingItemsAttachmentsListComponent extends LinearLayout {

	private Context context = null;
	
	private LinearLayout list = null;
	
	private List<MeetingItemsAttachmentInfo> meetingItemsAttachments = new ArrayList<MeetingItemsAttachmentInfo>();
	
	
	public MeetingItemsAttachmentsListComponent(Context context,AttributeSet attrs){	
		
		super(context,attrs);		
		
		final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.meetingattachments_list_component, this);
        
        this.context = context;                                      
        
        list =(LinearLayout) this.findViewById(R.id.list) ;
	
	}
	
	
	public void SetData(MeetingItemInfo item) throws SQLException, ParseException{
			
		meetingItemsAttachments = MeetingItemsAttachments.Instance.GetItemsByMeetingItemID(item.GetMeetingItemID());
		
		list.removeAllViews();
		
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
								
		for (final MeetingItemsAttachmentInfo meetingItemsAttachment : meetingItemsAttachments) {
			
			View v = inflater.inflate(R.layout.meetingitemattachments_list_item, null);
			
			TextView tvFileName = (TextView) v.findViewById(R.id.tvFileName);
			TextView tvFileSize = (TextView) v.findViewById(R.id.tvFileSize);
			
			long fileSize =  meetingItemsAttachment.GetFileSize()!=0?meetingItemsAttachment.GetFileSize()/1024l:0;
			
			tvFileName.setText(meetingItemsAttachment.GetFileName());			
			tvFileSize.setText(Long.toString(fileSize));
			
			v.setOnClickListener(new OnClickListener() {

		        public void onClick(View v) {
	        		        	   	               
	        		String filePath= MeetingItemsAttachments.Instance.GetFilePath(meetingItemsAttachment.GetMeetingItemID());
	        		Helper.OpenFile(filePath + meetingItemsAttachment.GetFileName());
		        }

		    });			
			
			list.addView(v);
		}
		

	}
	

	
}
